unit unitactividades;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  DbCtrls, sqldb, db, unitmainform;

type

  { TFormActividades }

  TFormActividades = class(TForm)
    Button1: TButton;
    Button2: TButton;
    DataSource1: TDataSource;
    DBComboBox1: TDBComboBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    SQLQuery1: TSQLQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    esp_nombre : string;
    dpt_cdg : string;
  end;

var
  FormActividades: TFormActividades;

implementation

{$R *.lfm}

{ TFormActividades }

procedure TFormActividades.FormCreate(Sender: TObject);
begin
  SQLQuery1.DataBase := MainForm.PQConnection1;
  SQLQuery1.Transaction := MainForm.SQLTransaction1;
end;

procedure TFormActividades.Button2Click(Sender: TObject);
begin
  Close();
end;

procedure TFormActividades.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  dpt_cdg := '';
  esp_nombre := '';
end;

procedure TFormActividades.Button1Click(Sender: TObject);
var
  QueryString : string;

begin
  Edit1.Text := uppercase(Edit1.Text);
  Edit2.Text := uppercase(Edit2.Text);
  Edit3.Text := uppercase(Edit3.Text);

  if FormActividades.Caption = 'Nueva Actividad' then
    QueryString := 'insert into deporte(dpt_nombre, dpt_descripcion, dpt_horarios, dpt_espacio) values('''
      + Edit1.Text + ''', ''' + Edit2.Text + ''', ''' + Edit3.Text + ''', ''' + DBComboBox1.Text + ''')'
  else
  if FormActividades.Caption = 'Modificar Actividad' then
    QueryString := 'update deporte set dpt_nombre = ''' + Edit1.Text + ''', dpt_descripcion = ''' + Edit2.Text
    + ''', dpt_horarios = ''' + Edit3.Text + ''', dpt_espacio = ''' + DBComboBox1.Text + ''' where dpt_cdg = ' + dpt_cdg;

  MainForm.PQConnection1.ExecuteDirect('BEGIN WORK;');
  MainForm.PQConnection1.ExecuteDirect(QueryString);
  MainForm.PQConnection1.ExecuteDirect('COMMIT WORK;');
  Close();
end;

procedure TFormActividades.FormShow(Sender: TObject);
begin
  if SQLQuery1.Active = true then
    SQLQuery1.Close;

  SQLQuery1.SQL.Text := 'select esp_nombre from espacio order by esp_nombre';

  SQLQuery1.Open;

  DBComboBox1.DataField := 'esp_nombre';
  DBComboBox1.Clear;

  while not SQLQuery1.EOF do
  begin
    DBComboBox1.Items.Add(SQLQuery1.Fields[0].AsString);
    SQLQuery1.Next;
  end;

  DBComboBox1.Text:= esp_nombre;

end;

end.

